#!/bin/bash
# 项目打包
# 在build文件夹下生成名为myrpc-service-${myrpc.version}.tar.bz2 的压缩包

sh_folder=$(cd "$(dirname $0)"; pwd -P)
file_name=myrpc-service-*-standalone*.jar
myrpc_version=
pushd $sh_folder 1>/dev/null

## 复制需要打包的文件到myrpc-service
function service_copy(){
    local folder="myrpc-service"
    # 从pom.xml中获取版本号
    local project_version=$myrpc_version
    rm -fr "$folder" || exit
    mkdir -p "$folder/target" || exit
   
    local jar_file="$sh_folder/target/myrpc-service-$project_version-standalone.jar"
    [ ! -f $jar_file ] && echo "没有找到目标jar包:$(basename $jar_file),$(basename $myrpc_version) 项目需要编译" && exit 255

    echo "copy file from $sh_folder/myrpc-service to $folder"
    # 优先复制PDF格式文档
    if [ -f "$sh_folder/README.pdf" ];
    then    
        cp -v "$sh_folder/README.pdf"    $folder || exit     
    elif [ -f "$sh_folder/README.md" ] ;
    then
        cp -v "$sh_folder/README.md"    $folder  || exit 
    fi
    cp -v "$sh_folder/install.sh"                          $folder         || exit
    cp -v "$sh_folder/myrpc"                               $folder         || exit
    cp -v "$sh_folder/myrpc.service.in"                    $folder         || exit
    cp -v "$sh_folder/start_myrpc_server.sh"               $folder/        || exit
    cp -v "$sh_folder/start_myrpc_server.bat"              $folder/        || exit
    cp -v "$sh_folder/start_myrpc_server_debug.sh"         $folder/        || exit
    cp -v "$sh_folder/start_myrpc_server_debug.bat"        $folder/        || exit
    cp -v "$sh_folder/target/start_myrpc_server.sh"        $folder/target/ || exit
    cp -v "$sh_folder/target/start_myrpc_server.bat"       $folder/target/ || exit
    cp -v "$sh_folder/target/start_myrpc_server_debug.sh"  $folder/target/ || exit
    cp -v "$sh_folder/target/start_myrpc_server_debug.bat" $folder/target/ || exit
    cp -v "$jar_file"                                      $folder/target/ || exit
}

# 替换指定文本文件的windows换行符('\r\n')为linux换行符('\n')
# $1 文本文件
function replace_with_unix_newline(){
    local kernelName="$(uname -s)"
    case "${kernelName}" in
        MINGW* | CYGWIN*)     
            [ ! -f "$1" ] && return
            echo "replace '\r\n' with '\n' in $1"
            sed -i 's/\r\n/\n/' $1 || exit
            ;;
        *)  ;;
    esac
}
# (递归)替换指定文件夹下所有sh脚本的windows换行符('\r\n')为linux换行符('\n')
# $1 文件夹, 文件夹不存在则返回
function replace_sh_with_unix_newline(){
    local kernelName="$(uname -s)"
    case "${kernelName}" in
        MINGW* | CYGWIN*)   
            [ ! -d "$1" ] && return
            for file in $1/*sh  ; do 
                echo "replace '\r\n' with '\n' in $file"
                sed -i 's/\r\n/\n/' $file || exit
            done
            # 递归子目录
            for sub in $(ls -F $1 |grep /$) ; 
            do
                replace_sh_with_unix_newline $1/$sub
            done
            ;;
        *)  ;;
    esac
}
[ ! -f $sh_folder/target/$file_name ] && echo "not build yet, run shade-package.sh firstly(项目还没有正常编译,请先执行shade-package.sh编译项目)" && exit 255

for file in ${sh_folder}/target/*; do  
    if [[ ${file##*/} == $file_name ]]; then
        path=${file##*/myrpc-service-}
        myrpc_version=${path%-standalone*}
        echo myrpc_version=$myrpc_version
        break
    fi
done  

# 如果 build 文件不存在则创建
[ -d build ] || mkdir build || exit
pushd build 1>/dev/null || exit
tar_folder=myrpc-service-$myrpc_version
# 生成tar包的位置(相对路径)
tar_file=$tar_folder.tar

echo "crate distributable package (创建发行包) $myrpc_version..."
[ ! -d $tar_folder ] || rm -fr $tar_folder || exit 

mkdir $tar_folder || exit
pushd $tar_folder 1>/dev/null  || exit 

# 复制运行jar包和脚本到 myrpc-service 文件夹
service_copy
# 将 myrpc-service 下的脚本文件替换为linux换行符
replace_with_unix_newline myrpc-service/myrpc
replace_with_unix_newline myrpc-service/myrpc.service.in
replace_sh_with_unix_newline myrpc-service
# tar folder
popd 1>/dev/null || exit
echo "生成压缩包 $tar_file ..."
tar cvf $tar_file $tar_folder || exit 

echo "生成压缩包 $tar_file.bz2 ..."

bzip2 -f $tar_file || exit
echo "distributable package is available(发行包已生成):$(pwd)/$tar_file.bz2 "
# build
popd 1>/dev/null 
# $sh_folder
popd 1>/dev/null 
